package com.example.tools.menu;

import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Properties;

public class docker extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        String filename = Messages.showInputDialog(project, "please Enter DockerFile File Name",
                "create DockerFile", Messages.getInformationIcon());
        if (StringUtils.isBlank(filename)) {
            NotificationGroupManager.getInstance().getNotificationGroup("notificationGroup")
                    .createNotification("filename cannot be null", NotificationType.WARNING)
                    .notify(project);
            return;
        }
        //读取当前文件夹路径
        PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
        //读取模板
        FileTemplate template = FileTemplateManager.getInstance(project)
                .getInternalTemplate("docker");
        Properties properties = new Properties();
        //文件模板替换参数
        properties.setProperty("port", "填写端口");
        String renderedText = "";
        try {
            renderedText += template.getText(properties);
            renderedText = renderedText.replaceAll("(\r\n)", "\n");
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(filename, PlainTextLanguage.INSTANCE, renderedText);
        try {
            //EXECUTE WRITE FILE
            Runnable runnable = () -> psiElement.add(psiFile);
            // IDE scheduling tasks
            WriteCommandAction.runWriteCommandAction(project, runnable);
            //jump file
            OpenFileDescriptor descriptor = new OpenFileDescriptor(project, psiFile.getVirtualFile());
            Editor editor = FileEditorManager.getInstance(project)
                    .openTextEditor(descriptor, true);
        } catch (Exception exception) {
            NotificationGroupManager.getInstance().getNotificationGroup("notificationGroup")
                    .createNotification("File already exists", NotificationType.INFORMATION)
                    .notify(project);
            Messages.showMessageDialog("File already exists", "info", MessageType.INFO.getDefaultIcon());
            return;
        }
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
        VirtualFile[] virtualFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
        if (virtualFiles != null && virtualFiles.length == 1) {
            if (virtualFiles[0].isDirectory()) {
                //FOLDER
                e.getPresentation().setEnabled(true);
            } else {
                //file
                e.getPresentation().setEnabled(false);
            }
        } else {
            //多个文件
            e.getPresentation().setEnabled(true);
        }
    }

}
